#!/bin/bash

# 修复所有使用@agent宏的文件的import问题

# 需要添加的import语句
IMPORT_LINES="import yao.core.agent.{StandardAgentParams, AgentParamsConverter, AdvancedAgentFactory}
import yao.core.agent.{AdvancedAgent, AgentToolManager, AgentInterceptor}"

# 需要修复的文件列表
FILES=(
    "src/examples/simple_agent_macro_test/pkg.cj"
    "src/examples/comprehensive_macro_demo/comprehensive_macro_demo.cj"
    "src/examples/advanced_macro_showcase/advanced_macro_showcase.cj"
    "src/examples/enhanced_agent_demo/enhanced_agent_demo.cj"
    "src/examples/dsl/advanced_macro_examples.cj"
    "src/examples/dsl/cangjie_magic_dsl_demo.cj"
    "src/examples/dsl/macro_dsl_demo.cj"
    "src/examples/dsl/real_agent_dsl_example.cj"
    "src/examples/dsl/real_macro_test.cj"
    "src/examples/dsl/simple_macro_demo.cj"
    "src/examples/dsl/simple_macro_test.cj"
    "src/examples/dsl/simple_real_macro_test.cj"
)

echo "开始修复import问题..."

for file in "${FILES[@]}"; do
    if [ -f "$file" ]; then
        echo "修复文件: $file"
        
        # 检查文件是否已经包含AdvancedAgent import
        if ! grep -q "AdvancedAgent" "$file"; then
            # 找到第一个import语句的行号
            import_line=$(grep -n "^import yao\." "$file" | head -1 | cut -d: -f1)
            
            if [ ! -z "$import_line" ]; then
                # 在第一个import后添加新的import
                sed -i.bak "${import_line}a\\
$IMPORT_LINES" "$file"
                echo "  ✅ 已添加AdvancedAgent相关import"
            else
                echo "  ❌ 未找到import语句"
            fi
        else
            echo "  ⏭️ 已包含AdvancedAgent import，跳过"
        fi
    else
        echo "  ❌ 文件不存在: $file"
    fi
done

echo "修复完成！"
